﻿using System;
using innovations.util.exts.mathdotnet;
using innovations.util.exts.msft;
using MathNet.Numerics.LinearAlgebra.Generic;

namespace innovations.ml.core
{
    public class Sigmoid
    {
        public static Matrix<double> Compute(Matrix<double> z)
        {
            int r = 1;
            return r.PointwiseDivision(r.PointwiseAddition((-r).PointwiseMultiplication(z).PointwiseExp()));
        }

        public static Vector<double> Compute(Vector<double> z)        
        {
            int r = 1;
            return r.PointwiseDivision(r.PointwiseAddition((-r).PointwiseMultiplication(z).PointwiseExp()));
        }

        public static double Compute(double z)
        {
            return 1 / (1 + (Math.Exp(-z)));
        }

        public static Matrix<double> ComputeGradient(Matrix<double> z)
        {
            int r = 1;
            return Sigmoid.Compute(z).PointwiseMultiply(r.PointwiseSubtraction(Sigmoid.Compute(z)));
        }

        public static Vector<double> ComputeGradient(Vector<double> z)
        {
            int r = 1;
            return Sigmoid.Compute(z).PointwiseMultiply(r.PointwiseSubtraction(Sigmoid.Compute(z)));
        }
    }
}
